package com.post.service.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.post.dao.BaseDaoI;
import com.post.model.Tdept;
import com.post.model.Tshuangchuang;
import com.post.pageModel.DataGrid;
import com.post.pageModel.Shuangchuang;
import com.post.service.ShuangchuangServiceI;

@Service("shuangchuangService")
public class ShuangchuangServiceImpl implements ShuangchuangServiceI {

	private BaseDaoI<Tshuangchuang> shuangchuangDao;
	private BaseDaoI<Tdept> deptDao;

	public BaseDaoI<Tdept> getDeptDao() {
		return deptDao;
	}

	@Autowired
	public void setDeptDao(BaseDaoI<Tdept> deptDao) {
		this.deptDao = deptDao;
	}

	public BaseDaoI<Tshuangchuang> getShuangchuangDao() {
		return shuangchuangDao;
	}

	@Autowired
	public void setShuangchuangDao(BaseDaoI<Tshuangchuang> shuangchuangDao) {
		this.shuangchuangDao = shuangchuangDao;
	}

	@Override
	public void add(Shuangchuang shuangchuang) {
		Tshuangchuang t = new Tshuangchuang();
		BeanUtils.copyProperties(shuangchuang, t);
		t.setTdept(deptDao.get(Tdept.class, shuangchuang.getDeptId()));
		t.setCid(UUID.randomUUID().toString());
		t.setCstatus(1);//双创项目状态设为有效，2代表已完成
		shuangchuangDao.save(t);
	}

	@Override
	public void edit(Shuangchuang shuangchuang) {
		Tshuangchuang t = shuangchuangDao.get(Tshuangchuang.class, shuangchuang.getCid());
		BeanUtils.copyProperties(shuangchuang, t,new String[]{"cid","cstatus"});
	}

	@Override
	public void delete(Shuangchuang shuangchuang) {
		String cids = "";
		if (shuangchuang.getIds() != null) {
			String ids = shuangchuang.getIds();
			for (String cid : ids.split(",")) {
				cids += "'" + cid + "'" + ",";
			}
			cids = cids.substring(0, cids.length() - 1);
			String hql = "delete Tshuangchuang t where t.cid in (" + cids + ")";
			shuangchuangDao.executeHql(hql);
		}

	}

	@Override
	public DataGrid datagrid(Shuangchuang shuangchuang) {
		DataGrid dg = new DataGrid();
		dg.setRows(listRows(find(shuangchuang)));
		dg.setTotal(total(shuangchuang));
		return dg;
	}

	private Long total(Shuangchuang shuangchuang) {
		String hql = "select count(*) from Tshuangchuang t where 1=1";
		Map<String, Object> params = new HashMap<String, Object>();
		hql = addWhere(shuangchuang, hql, params);
		return shuangchuangDao.count(hql, params);
	}

	private List<Tshuangchuang> find(Shuangchuang shuangchuang) {
		String hql = "from Tshuangchuang t where 1=1";
		Map<String, Object> params = new HashMap<String, Object>();
		hql = addWhere(shuangchuang, hql, params);
		if (shuangchuang.getOrder() != null && shuangchuang.getSort() != null) {
			hql += " order by " + shuangchuang.getSort() + " " + shuangchuang.getOrder();
		}
		return shuangchuangDao.find(hql, params, shuangchuang.getPage(), shuangchuang.getRows());
	}

	private String addWhere(Shuangchuang shuangchuang, String hql, Map<String, Object> params) {
		if (shuangchuang != null) {
			if (shuangchuang.getDeptId() != null
					&& deptDao.get(Tdept.class, shuangchuang.getDeptId()).getTdept() != null) {
				hql += " and t.tdept.cid= :deptid ";
				params.put("deptid", shuangchuang.getDeptId());
			}
			if (shuangchuang.getCstartdate()!=null){
				hql +=" and t.cstartdate >= :cstartDate";
				params.put("cstartDate", shuangchuang.getCstartdate());
			}
			if (shuangchuang.getCenddate()!=null){
				hql +=" and t.cenddate <= :cendDate";
				params.put("cendDate", shuangchuang.getCenddate());
			}
			if (shuangchuang.getCstatus()!=null && shuangchuang.getCstatus() > 0){
				hql += " and t.cstatus = :status";
				params.put("status", shuangchuang.getCstatus());
			}
			if (shuangchuang.getCname()!=null && !shuangchuang.getCname().trim().equals("")){
				hql +=" and t.cname like :cname";
				params.put("cname", "%%"+shuangchuang.getCname()+"%%");
			}
		}
		return hql;
	}

	private List<Shuangchuang> listRows(List<Tshuangchuang> lt) {
		List<Shuangchuang> rows = new ArrayList<Shuangchuang>();
		if (lt != null && lt.size() > 0) {
			for (Tshuangchuang t : lt) {
				Shuangchuang sub = new Shuangchuang();
				BeanUtils.copyProperties(t, sub);
				if (t.getTdept() != null) {
					sub.setDeptName(t.getTdept().getCdeptName());
				}
				rows.add(sub);
			}
		}
		return rows;
	}

	@Override
	public List<Shuangchuang> combobox() {
		List<Shuangchuang> rl = new ArrayList<Shuangchuang>();
		List<Tshuangchuang> l = shuangchuangDao.find("from Tshuangchuang");
		if (l != null && l.size() > 0) {
			for (Tshuangchuang t : l) {
				Shuangchuang r = new Shuangchuang();
				r.setCid(t.getCid());
				r.setCname(t.getCname());
				rl.add(r);
			}
		}
		return rl;
	}

	@Override
	public void finish(Shuangchuang shuangchuang) {
		Tshuangchuang t = shuangchuangDao.get(Tshuangchuang.class, shuangchuang.getCid());
		t.setCstatus(2);
	}


}
